package fts.ftsmonitoring.ftsTaggers;


import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import fts.ftsmonitoring.ftsTaggers.IAutoTagger;



public class FileNameKeywordTagger implements IAutoTagger
{
	HashMap<String, String> extTagsMap = new HashMap<String, String>();
	HashMap<String, String> keywordsMap = new HashMap<String, String>();
	public FileNameKeywordTagger()
	{
		extTagsMap.put("jpg", "image");
		extTagsMap.put("jpeg", "image");
		extTagsMap.put("gif", "image");
		extTagsMap.put("giff", "image");
		extTagsMap.put("png", "image");
		extTagsMap.put("tif", "image");
		extTagsMap.put("tiff", "image");
		extTagsMap.put("ico", "icon");
		extTagsMap.put("txt", "document");
		extTagsMap.put("doc", "document");
		extTagsMap.put("rtf", "document");
		extTagsMap.put("mp3", "audio");
		extTagsMap.put("wav", "audio");
		
		keywordsMap.put("heb", "hebrew");
		keywordsMap.put("hebrew", "hebrew");
		keywordsMap.put("birthday", "birthday");
		keywordsMap.put("java", "development");
		keywordsMap.put("c#", "development");
		keywordsMap.put(".net", "development");
		keywordsMap.put("framework", "development");
		keywordsMap.put("groovy", "development");
		keywordsMap.put("scala", "development");
		keywordsMap.put("c++", "development");
	}

	public String getDescription()
	{
		return "Tags files using keywords (by filename)";
	}

	public ArrayList<String> getTags(String fileName)
	{
		ArrayList<String> res = new ArrayList<String>();
		File f = new File(fileName);
		fileName = fileName.toLowerCase();
		if(f.exists())
		{
			int extInd = fileName.lastIndexOf(".");
			if(extInd > 0 && (extInd < fileName.length() -1))
			{
				String ext = fileName.substring(extInd + 1);
				fileName = fileName.substring(0,extInd);
				if(extTagsMap.containsKey(ext))
				{
					res.add(extTagsMap.get(ext));
				}
			}
			
			Iterator<String> iter = keywordsMap.keySet().iterator();
			while(iter.hasNext())
			{
				String key = iter.next();
				if(fileName.indexOf(key) >= 0)
				{
					String val = keywordsMap.get(key);
					if(!res.contains(val))
					{
						res.add(val);
					}
				}
			}
		}
		return res;
	}

}
